const Router = require('koa-router');
const router = new Router();
// 删除角色
const {db, Op, QueryTypes} = require("../models/db");
const tool = require("../tools/tool");
router.post('/role/roleDel', async ctx => {
    let ids = ctx.request.body.ids
    // let roleId = ctx.request.body.roleId
    if(ids.length > 0) {
        await db.transaction(async (t) => {
            await db.models['roleMenu'].destroy({
                where: {
                    roleId: {
                        [Op.in]: ids
                    }
                }
            }, {transaction: t})
            await db.models['role'].destroy({
                where: {
                    id: {
                        [Op.in]: ids
                    }
                }
            }, {transaction: t})
        })
    }
    return ctx.suc('已删除')
})
router.post('/role/roleList', async ctx => {
    let tid = ctx.user.tenantId
    let sql = `select id,name from role where tenantId=${tid} and isDel=0 `
    let ret = tool.page(ctx, sql)
    return ret
})
// 保存角色的菜单权限
router.post('/role/saveRoleMenu', async ctx => {
    let formData = ctx.request.body.formData
    let roleId = ctx.request.body.roleId
    if(roleId) {
        await db.models['roleMenu'].destroy({
            where: {
                roleId: roleId
            }
        })
        await db.models['roleMenu'].bulkCreate(formData)
    }
    return ctx.suc('已保存')
})
router.post('/role/tenant', async ctx => {
    let sql = `select id,name,state,mobile from tenant where dt is null  `
    let ret = tool.page(ctx, sql)
    return ret
})
// 保存租户
router.post('/user/saveTenant', async ctx => {
    let formData = ctx.request.body.formData
    let mobile = /^1[3-9][0-9]{9}$/.test(formData.mobile)
    if(!mobile) return ctx.err('手机号码不正确')
    // 检测是否有相同手机号
    let c = await db.models['tenant'].count({
        where: {
            mobile: formData.mobile,
        }
    })
    if(c > 0) return ctx.err('已存在相同手机号')
    let a = await db.models['admin'].count({
        where: {
            username: formData.mobile,
        }
    })
    if(a > 0) return ctx.err('已存在相同手机号')
    if(formData.id) {
        db.models['tenant'].update(formData, {
            where: {
                id: formData.id
            }
        })
    }
    else {
        let tenant = await db.transaction(async t => {
            let model = await db.models['tenant'].create(formData, {transaction: t}) // 新增公司
            let admin = {
                tenantId: model.id,
                mobile: formData.mobile,
                username: formData.mobile,
                userpwd: tool.crypto.MD5('121121'),
                token: tool.uuid(),
                name: formData.name,
                pinYin: tool.py(formData.name),
                roleName: '管理员',
                cby: ctx.user.id,
                ctime: tool.getTime()
            }
            await db.models['admin'].create(admin, {transaction: t})
            let sql = `exec to_init_default_data @tenantId=${model.id} `
            await db.query(sql, {type: QueryTypes.UPDATE}, {transaction: t})
            return model
        })
        if(tenant) {
            return  ctx.suc('创建成功')
        }
        return ctx.err('创建失败')
    }
})

module.exports = router;